package com.zzyl.nursing.service.impl;

import java.time.LocalDateTime;
import java.util.HashMap;
import java.util.List;

import cn.hutool.core.date.LocalDateTimeUtil;
import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zzyl.common.utils.DateUtils;
import com.zzyl.common.utils.UserThreadLocal;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.zzyl.nursing.mapper.ReservationMapper;
import com.zzyl.nursing.domain.Reservation;
import com.zzyl.nursing.service.IReservationService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import java.util.Arrays;
import java.util.Map;

/**
 * 预约信息Service业务层处理
 *
 * @author ruoyi
 * @date 2025-02-27
 */
@Service
public class ReservationServiceImpl extends ServiceImpl<ReservationMapper, Reservation> implements IReservationService {
    @Autowired
    private ReservationMapper reservationMapper;

    /**
     * 查询预约信息
     *
     * @param id 预约信息主键
     * @return 预约信息
     */
    @Override
    public Reservation selectReservationById(Long id) {
        return getById(id);
    }

    /**
     * 查询预约信息列表
     *
     * @param
     * @return 预约信息
     */
    @Override
    public List<Reservation> selectReservationList(Integer pageNum, Integer pageSize, Integer status) {
        List<Reservation> records = this.lambdaQuery().eq(ObjectUtil.isNotEmpty(status), Reservation::getStatus, status)
                .eq(Reservation::getCreateBy, UserThreadLocal.getUserId())
                .page(Page.of(pageNum, pageSize)).getRecords();
        return records;
    }

    /**
     * 新增预约信息
     *
     * @param reservation 预约信息
     * @return 结果
     */
    @Override
    public int insertReservation(Reservation reservation) {
        reservation.setCreateBy(String.valueOf(UserThreadLocal.getUserId()));
        return save(reservation) ? 1 : 0;
    }

    /**
     * 修改预约信息
     *
     * @param
     * @return 结果
     */
    @Override
    public int updateReservation(Long id) {
        boolean update = lambdaUpdate().eq(Reservation::getId, id)
                .set(Reservation::getStatus, 2)
                .update();
        return update ? 1 : 0;
    }

    /**
     * 批量删除预约信息
     *
     * @param ids 需要删除的预约信息主键
     * @return 结果
     */
    @Override
    public int deleteReservationByIds(Long[] ids) {
        return removeByIds(Arrays.asList(ids)) ? 1 : 0;
    }

    /**
     * 删除预约信息信息
     *
     * @param id 预约信息主键
     * @return 结果
     */
    @Override
    public int deleteReservationById(Long id) {
        return removeById(id) ? 1 : 0;
    }

    /**
     * 查询取消预约数量
     *
     * @param
     * @return
     */
    @Override
    public int getCancelledReservationCount(Long id) {
        // 获取当天开始时间和结束时间
        long time = System.currentTimeMillis();
        LocalDateTime ldt = LocalDateTimeUtil.of(time);
        LocalDateTime startTime = ldt.toLocalDate().atStartOfDay();
        LocalDateTime endTime = startTime.plusDays(1);
        return reservationMapper.getCancelledReservationCount(id, startTime, endTime);
    }

    /**
     * 查询每个时间段剩余的预约数量
     *
     * @param userId
     * @param time
     * @return
     */
    @Override
    public List<Map> getReservationCountByTime(Long userId, long time) {
        return reservationMapper.getReservationCountByTime(userId, time);
    }

    /**
     * 更新预约状态
     */
    @Override
    public void updateReservationStatus() {
        this.lambdaUpdate().set(Reservation::getStatus, 3)
                .eq(Reservation::getStatus, 0)
                .lt(Reservation::getTime, LocalDateTime.now())
                .update();
    }


}
